#ifndef __YX020_PORT_H
#define __YX020_PORT_H

#include "stdint.h"

#define YX020PORT_BIT_CHECK(val,x)  	((val & x) == x ? 1 : 0)

#define YX020PORT_FRAME_NORMAL		0x00
#define YX020PORT_FRAME_BOARD_TEST0	0x01
#define YX020PORT_FRAME_BOARD_TEST1 0x02

#define YX020PORT_LOWIQ_DISABLE		0x00
#define YX020PORT_LOWIQ_ENABLE		0x01

#define YX020PORT_8BIT_DISABLE		0x00
#define YX020PORT_8BIT_ENABLE		0x01

#define YX020PORT_SENDCACHE_SIZE 	64

typedef void (* DelayMsFunction)(uint32_t ms);

typedef enum {
    YX020_BROAD_REG0 = 0x61,
    YX020_BROAD_REG1 = 0x63,
    YX020_BROAD_REG2 = 0x65,
    YX020_BROAD_REG3 = 0x67,
    YX020_BROAD_REG4 = 0x69,
    YX020_BROAD_REG5 = 0x6B,
    YX020_BROAD_REG6 = 0x6D,
    YX020_BROAD_REG7 = 0x6F
} YX020_BROAD_REG_E;

typedef enum {
    YX020_WSELECT_REG0 = 0x60,
    YX020_WSELECT_REG1 = 0x62,
    YX020_WSELECT_REG2 = 0x64,
    YX020_WSELECT_REG3 = 0x66,
    YX020_WSELECT_REG4 = 0x68,
    YX020_WSELECT_REG5 = 0x6A,
    YX020_WSELECT_REG6 = 0x6C,
    YX020_WSELECT_REG7 = 0x6E
} YX020_WSELECT_REG_E;

void YX020PortInit(DelayMsFunction delayMs);
void YX020PortSetDelayMs(DelayMsFunction delayMs);
void YX020PortSetLowiq(uint8_t status);
void YX020PortSet8bitmode(uint8_t status);
void YX020PortSetHLBaud(uint32_t low, uint32_t high);

void YX020PortTimerSetFre(uint32_t fre);
void YX020PortTimerRun(void);
void YX020PortTimerStop(void);

uint8_t YX020PortCheckDir(void);
void YX020PortSetUartDir(uint8_t dir);

void YX020PortSendCMD2S(void);
void YX020PortSendCMD2D(void);
void YX020PortSendVSYNC(void);
void YX020PortSendSetBaud(void);
void YX020PortSendSetID(uint16_t num);
void YX020PortSendBrightness(uint16_t id, const uint16_t *br, uint16_t len);
void YX020PortSendBrightness8bit(uint16_t id, const uint8_t *br, uint16_t len);
void YX020PortBroacastBr(uint16_t *br, uint8_t vsync);
void YX020PortReadREG(uint16_t id, uint8_t reg);
void YX020PortBroadcastREG(YX020_BROAD_REG_E reg, uint8_t val);
void YX020PortWriteSelectReg(YX020_WSELECT_REG_E reg, uint8_t val, uint16_t id, uint8_t cmd2s);
void YX020PortSendFrameHead(uint8_t reg4, uint8_t reg7, uint8_t tm);
void YX020PortChipInit(uint8_t reg4, uint16_t chipNum, uint8_t btm, uint8_t reg7, uint8_t cmd2d);
void YX020PortFrame(const uint8_t *regList, const uint16_t *brData, uint16_t brLen, uint16_t startID);
void YX020PortFrameHLBaud(const uint8_t *regs, const uint16_t *br, uint16_t brLen, uint16_t id, uint8_t f0);

#endif
